/*----------------------------------------------------------------------
  - File name      : STC8Ax_TIMER.c
  - Author         : Quark Team
  - Update date    : 2020-08-30                   
  -	Copyright      : Gevico Electronic studio   
  - Module comments: TIMER module.
-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
|                               INCLUDES                                |
-----------------------------------------------------------------------*/
#include "STC8Ax_TIMER.h"
/*-----------------------------------------------------------------------
|                                 DATA                                  |
-----------------------------------------------------------------------*/

/*--------------------------------------------------------
| @Description: TIMER mode configure define function     |
--------------------------------------------------------*/

#define TIMER0_MODE_CFG(Mode)  TMOD = (TMOD & 0xFC) | (Mode)
#define TIMER1_MODE_CFG(Mode)  TMOD = (TMOD & 0xCF) | (Mode << 4)

/*--------------------------------------------------------
| @Description: TIMER clk mode configure define function |
--------------------------------------------------------*/

#define  TIMER0_TCY_CFG(TCycle) AUXR = (AUXR & 0x7F) | (TCycle << 7)
#define  TIMER1_TCY_CFG(TCycle) AUXR = (AUXR & 0xBF) | (TCycle << 6)
#define  TIMER2_TCY_CFG(TCycle) AUXR = (AUXR & 0xFB) | (TCycle << 2)
#define  TIMER3_TCY_CFG(TCycle) T4T3M = (T4T3M & 0xFD) | (TCycle << 1)
#define  TIMER4_TCY_CFG(TCycle) T4T3M = (T4T3M & 0xDF) | (TCycle << 5)

/*--------------------------------------------------------
| @Description: TIMER Value configure define function    |
--------------------------------------------------------*/

#define TIMER0_VALUE_CFG(Value) { \
Value = (65536UL - (SYSCLK_FRE / (1000000UL /  Value \
	         * (11 * (!(AUXR & 0x80)) + 1)) ) );}

#define TIMER1_VALUE_CFG(Value) { \
Value = (65536UL - (SYSCLK_FRE / (1000000UL /  Value \
	         * (11 * (!(AUXR & 0x40)) + 1)) ) );}

#define TIMER2_VALUE_CFG(Value) { \
Value = (65536UL - (SYSCLK_FRE / (1000000UL /  Value \
	         * (11 * (!(AUXR & 0x04)) + 1)) ) );}

#define TIMER3_VALUE_CFG(Value) { \
Value = (65536UL - (SYSCLK_FRE / (1000000UL /  Value \
	         * (11 * (!(T4T3M & 0x02)) + 1)) ) );}	

#define TIMER4_VALUE_CFG(Value) { \
Value = (65536UL - (SYSCLK_FRE / (1000000UL /  Value \
	         * (11 * (!(T4T3M & 0x20)) + 1)) ) );}	
             
/*-----------------------------------------------------------------------
|                               FUNCTION                                |
-----------------------------------------------------------------------*/

/*--------------------------------------------------------
| @Description: TIMER0 peripheral init function          |
| @param      : const TIMER_InitType *TIMERx             |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE TIMER0_Init(const TIMER_InitType *TIMERx)
{
	TR0 = 0; //Turn off Timer0
	TMOD = (TMOD & 0xFB) | (TIMERx -> Type << 2);
	TIMER0_MODE_CFG(TIMERx -> Mode);
	TIMER0_TCY_CFG(TIMERx -> TCycle);
	TIMER0_VALUE_CFG(TIMERx -> Value);
	if(TIMERx -> Mode == TIMER_8BitAutoReload) T0L = (uint8)(TIMERx -> Value), T0H = (uint8)(TIMERx -> Value); 
	else                                       T0L = (uint8)(TIMERx -> Value), T0H = (uint8)(TIMERx -> Value >> 8);
	INTCLKO = (INTCLKO & 0xFE) | (TIMERx -> ClkOut);
	TR0 = TIMERx -> Run; 
	return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: TIMER1 peripheral init function          |
| @param      : const TIMER_InitType *TIMERx             |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE TIMER1_Init(const TIMER_InitType *TIMERx)
{
	TR1 = 0; //Turn off Timer1
	if(TIMERx -> Mode != TIMER_16BitAutoReloadNoMask) TIMER1_MODE_CFG(TIMERx -> Mode);
	else return FSC_FAIL;
	TMOD = (TMOD & 0xBF) | (TIMERx -> Type << 6);
	TIMER1_TCY_CFG(TIMERx -> TCycle);
	TIMER1_VALUE_CFG(TIMERx -> Value);	
	if(TIMERx -> Mode == TIMER_8BitAutoReload) T1L = (uint8)(TIMERx -> Value), T1H = (uint8)(TIMERx -> Value); 
	else                                       T1L = (uint8)(TIMERx -> Value), T1H = (uint8)(TIMERx -> Value >> 8);
	INTCLKO = (INTCLKO & 0xFD) | (TIMERx -> ClkOut << 1);
	TF1 = 0; //Clear Flag
	TR1 = TIMERx -> Run; 
	return FSC_SUCCESS;		
}

/*--------------------------------------------------------
| @Description: TIMER2 peripheral init function          |
| @param      : const TIMER_InitType *TIMERx             |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE TIMER2_Init(const TIMER_InitType *TIMERx)
{
	AUXR &= 0xEF; //Turn off timer2
	AUXR = (AUXR & 0xF7) | (TIMERx -> Type << 3);
	TIMER2_TCY_CFG(TIMERx -> TCycle);
	TIMER2_VALUE_CFG(TIMERx -> Value);	
	T2L = (uint8)(TIMERx -> Value);
	T2H = (uint8)(TIMERx -> Value >> 8);
	INTCLKO = (INTCLKO & 0xFB) | (TIMERx -> ClkOut << 2);	
	AUXR = (AUXR & 0xEF) | (TIMERx -> Run << 4);  
	return FSC_SUCCESS;	
}

/*--------------------------------------------------------
| @Description: TIMER3 peripheral init function          |
| @param      : const TIMER_InitType *TIMERx             |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE TIMER3_Init(const TIMER_InitType *TIMERx)
{
	T4T3M &= 0XF7;//Turn off timer3
	T4T3M = (T4T3M & 0xFB) | (TIMERx -> Type << 2);
	TIMER3_TCY_CFG(TIMERx -> TCycle);
	TIMER3_VALUE_CFG(TIMERx -> Value);
	T3L = (uint8)(TIMERx -> Value);
	T3H = (uint8)(TIMERx -> Value >> 8);
	T4T3M = (T4T3M & 0xFE) | (TIMERx -> ClkOut);
	T4T3M = (T4T3M & 0xF7) | (TIMERx -> Run << 3);		
	return FSC_SUCCESS;	
}

/*--------------------------------------------------------
| @Description: TIMER4 peripheral init function          |
| @param      : const TIMER_InitType *TIMERx             |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE TIMER4_Init(const TIMER_InitType *TIMERx)
{
	T4T3M &= 0X7F; //Turn off timer4
	T4T3M = (T4T3M & 0xBF) | (TIMERx -> Type << 6);
	TIMER4_TCY_CFG(TIMERx -> TCycle);
	TIMER4_VALUE_CFG(TIMERx -> Value);	
	T4L = (uint8)(TIMERx -> Value);
	T4H = (uint8)(TIMERx -> Value >> 8);
	T4T3M = (T4T3M & 0xEF) | (TIMERx -> ClkOut << 4);
	T4T3M = (T4T3M & 0x7F) | (TIMERx -> Run << 7);	
	return FSC_SUCCESS;		
}

/*--------------------------------------------------------
| @Description: TIMER5 peripheral Drivice function       |
| @param      : uint16 Value                             |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE TIMER5_Wake_Up_Power(uint16 Value,FUNSTATE State)
{
	uint16 FWT;
	FWT = (uint16)((FWTH << 8) | FWTL);    //Calculate the clock frequency 
	Value = (uint16)((FWT * (uint32) Value) / 16000000UL);//Count count times
	WKTCH = (uint8)((Value - 1) >> 8);
	WKTCL = (uint8)( Value - 1);
	if(State == ENABLE) WKTCH |= 0x80;  //Enable the power down wake-up timer
	else                WKTCH &= 0x7F;  //Disable the power down wake-up timer
	return FSC_SUCCESS;
}



/*-----------------------------------------------------------------------
|          END OF FLIE        (C) COPYRIGHT Gevico Electronics          | 
-----------------------------------------------------------------------*/



